package com.englisheveryday.db;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.englisheveryday.constants.Constants;
import com.englisheveryday.utils.L;
import com.englisheveryday.utils.StringUtil;
import com.englisheveryday.vo.ArticleVO;
import com.englisheveryday.vo.CategoryVO;

/**
 * <pre>
 *    
 * </pre>
 * @author 马永华
 * @date 2012-9-8 下午10:26:50
 * @version 1.1
 */

public class CategoryDAO  {

	private DBHelper  dbHelper;
	private List<String> errorIdList;
	
	public CategoryDAO(Context context){
		dbHelper = new DBHelper(context, "/data/data/com.englisheveryday/englishdata.db");
		errorIdList = new ArrayList<String>();
	}
	
	
	//保存每日口语信息
	public void insert(CategoryVO vo,SQLiteDatabase db){
		String id = "";
		try{
		ContentValues cv = new ContentValues();
		cv.put("name", vo.getName());
		cv.put("id",vo.getId());
		L.d(vo.getName()+" "+vo.getId());
		id = vo.getId();
		if(findbyid(vo.getId(),db)>0){
			L.d("已经存在了");
			return;
		}
		long l = db.insert("category", null, cv);
		L.d("插入:"+l);
		}catch(Exception e){
			e.printStackTrace();
			errorIdList.add(id);
		}
	}
	
	//更新每日口语信息
	public void update(CategoryVO vo,SQLiteDatabase db){
		String id = "";
		try{
			ContentValues cv = new ContentValues();
			cv.put("name", vo.getName());
			id = vo.getId();
			if(findbyid(vo.getId(),db)==0){
				L.d("不存在");
				return;
			}
			long l = db.update("category", cv,"id=?", new String[]{vo.getId()});
			L.d("更新："+l);
		}catch(Exception e){
			e.printStackTrace();
			errorIdList.add(id);
		}
		
	}
	
	//批量更新
	public void sync(List<CategoryVO> list,long nextAnchor){
		SQLiteDatabase db = null;
		errorIdList.clear();
		try {
			db = dbHelper.getWritableDatabase();
			for(CategoryVO vo : list){
				if(vo!=null&&"A".equals(vo.getType())){
					insert(vo,db);
				}else if(vo!=null&&"D".equals(vo.getType())){
					delete(vo.getId(),db);
				}else if(vo!=null&&"U".equals(vo.getType())){
					update(vo,db);
				}
			}
			L.d("失败："+errorIdList.size());
			//更新时间戳
			updateAnchor(nextAnchor,db);
		}catch(Exception e){
			e.printStackTrace();
		}finally {
			if(db!=null&&db.isOpen())
				db.close();
		}
	}
	
	//删除
	public int delete(String id,SQLiteDatabase db){
		int count = 0;
		try{
			count = db.delete("category", "id=?",new String[]{id});
		}catch(Exception e){
			e.printStackTrace();
			errorIdList.add(id);
		}
		return count;
	}
	
	public int delete(String id){
		int count = 0;
		SQLiteDatabase db = null;
		try{
			db = dbHelper.getWritableDatabase();
			count = db.delete("category", "id=?",new String[]{id});
		}catch(Exception e){
			e.printStackTrace();
		}finally {
			if(db!=null&&db.isOpen())
				db.close();
		}
		return count;
	}
	
	//查询是否存在
	public int findbyid(String id,SQLiteDatabase db){
		Cursor cursor = db.rawQuery(Constants.CATEGORY_SELECT_BY_ID, new String[]{id});
		int count = cursor.getCount();
		return count;
	}
	
	//查询所有
	public List<CategoryVO> findAll(){
		List<CategoryVO> categoryList = new  ArrayList<CategoryVO>();
		SQLiteDatabase db = null;
		Cursor cursor = null;
		try{
	    db = dbHelper.getWritableDatabase();
	    cursor = db.rawQuery(Constants.CATEGORY_SELECT_ALL,null);
	    L.d(cursor.getCount()+"");
		while(cursor.moveToNext()){
			CategoryVO vo = new CategoryVO();
			vo.setName(StringUtil.replaceNull(cursor.getString(cursor.getColumnIndex("name"))));
			vo.setId(StringUtil.replaceNull(cursor.getString(cursor.getColumnIndex("id"))));
			categoryList.add(vo);
		}
		}catch(Exception e){
			e.printStackTrace();
		}finally {
			if(db!=null&&db.isOpen())
				db.close();
			if(cursor!=null)
				cursor.close();
		}
		return categoryList;
	}
	
	
	//查询上次更新的时间戳
	public long queryAnchor(){
		SQLiteDatabase db = null;
		long anchor = 0;
		try{
			db = dbHelper.getWritableDatabase();
			Cursor cursor = db.rawQuery("select anchor from updatelog where type=?",new String[]{Constants.ANCHOR_CATEGORY});
			if(cursor!=null&&cursor.getCount()>0){
				cursor.moveToFirst();
				anchor = cursor.getLong(cursor.getColumnIndex("anchor"));
				L.d("an:"+anchor);
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally {
			if(db!=null&&db.isOpen())
				db.close();
		}
		return anchor;
	}
	
	
	//更新本次时间戳
	public int updateAnchor(long anchor,SQLiteDatabase db){
		int count  = 0;
		if(anchor==0)
			return count;
		try{
			ContentValues cv = new ContentValues();
			cv.put("anchor", anchor);
			count = db.update("updatelog", cv, "type=?",new String[]{Constants.ANCHOR_CATEGORY});
			L.d("anchor"+count);
		}catch(Exception e){
			e.printStackTrace();
		}
		return count;
	}
}

